package com.example.arithmeticleetcode.leetcode.nov;

import java.util.Arrays;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-12-01 09:15
 **/
public class Demo23 {

    public static void main(String[] args) {
        int[] nums = {3, 5, 2, 6};
        int[] nums2 = {2, 4, 3, 3, 5, 4, 9, 6};
        int[] nums3 = {3, 4, 2, 1};
        int[] nums4 = {71, 18, 52, 29, 55, 73, 24, 42, 66, 8, 80, 2};
        int[] nums5 = {84, 10, 71, 23, 66, 61, 62, 64, 34, 41, 80, 25, 91, 43, 4, 75, 65, 13, 37, 41, 46, 90, 55, 8, 85, 61, 95, 71};
        System.out.println(Arrays.toString(mostCompetitive(nums, 2)));
        System.out.println(Arrays.toString(mostCompetitive(nums2, 4)));
        System.out.println(Arrays.toString(mostCompetitive(nums3, 2)));
        System.out.println(Arrays.toString(mostCompetitive(nums4, 3)));
        System.out.println(Arrays.toString(mostCompetitive(nums5, 24)));
    }

    public static int[] mostCompetitive(int[] nums, int k) {
        int[] result = new int[k];

        int minValue = nums[0];
        int minIndex = 0;
        for (int i = 1, len = nums.length - k; i <= len; i++) {
            if (minValue > nums[i]) {
                minValue = nums[i];
                minIndex = i;
            }
        }
        int index = 0;
        result[index++] = nums[minIndex++];
        if ((nums.length - minIndex + 1) == k) {
            while (minIndex < nums.length) {

                result[index++] = nums[minIndex++];
            }
            return result;
        }
        while (index < k) {
            int temp = nums[minIndex++];
            for (int i = minIndex, len = nums.length; i < len; i++) {
                if (temp >= nums[i]) {
                    temp = nums[i];
                    minIndex = i;
                }
            }
            result[index++] = temp;

        }
        return result;


    }


    public static int[] mostCompetitive2(int[] nums, int k) {
        int[] result = new int[k];

        int minValue = nums[0];
        int minIndex = 0;
        for (int i = 1, len = nums.length - k; i <= len; i++) {
            if (minValue > nums[i]) {
                minValue = nums[i];
                minIndex = i;
            }
        }

//        Arrays.sort();

        int index = 0;
        result[index++] = nums[minIndex++];
        if ((nums.length - minIndex + 1) == k) {
            while (minIndex < nums.length) {

                result[index++] = nums[minIndex++];
            }
            return result;
        }
        while (index < k) {
            int temp = nums[minIndex++];
            for (int i = minIndex, len = nums.length; i < len; i++) {
                if (temp >= nums[i]) {
                    temp = nums[i];
                    minIndex = i;
                }
            }
            result[index++] = temp;

        }
        return result;


    }

    public int maxRepeating(String sequence, String word) {
        int count = 0;
        StringBuilder sb = new StringBuilder(word);
        while (sequence.contains(sb.toString())) {
            sb.append(word);
            count++;
        }
        return count;

    }
}
